# _*_ coding : utf-8 _*_
# @Time : 2022/1/11 0011 19:38
# @Author : 7.xpath解析案例
# @Project : 小猿圈爬虫--4k图片解析爬取

import requests
import os
from lxml import etree
if __name__=="__main__":
    #UA伪装：将对应的User-Agent封装到一个字典中
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
    }
    #指定url路径
    url = 'http://pic49.huitu.com/pic/20160121/551553_20160121213022788160_0.jpg'
    # 获取响应数据 text(字符串) content(二进制)  json(对象)
    response = requests.get(url=url,headers=headers)
    #手动设定响应数据的编码格式
    # requests.encoding = 'utf-8'
    img_data = response.content

    #创建一个文件夹
    if not os.path.exists('./picLibs'):
        os.mkdir('./picLibs')
    img_path = './picLibs/tu_0.jpg'
    with open(img_path,'wb') as fp:
        fp.write(img_data)
    print('保存成功')